package com.yichi;

import android.graphics.Rect;
import android.os.Bundle;
import android.security.keystore.StrongBoxUnavailableException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.yichi.adapter.OperateAdapter;
import com.yichi.device_bean.BatteryStore;
import com.yichi.global.EventDef;
import com.yichi.model.BatteryModel;
import com.yichi.service.DeviceService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class OperateFragment extends Fragment {
    private RecyclerView recyclerView;
    OperateAdapter operateAdapter;
    private ArrayList<BatteryModel> batteryModels;
    private Integer opsType = 0;//1取 2存

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_operate, container, false);
        recyclerView = view.findViewById(R.id.recycler_view);
        setView();
        registerEvent();
        return view;
    }

    Observer<String> observerOk = new Observer<String>() {
        @Override
        public void onChanged(String s) {
            if (isAdded()) {
                requireActivity().runOnUiThread(()->{
                    removeAllItemClick();
                });
            }
        }
    };

    private void removeAllItemClick() {
        for (BatteryModel batteryModel : batteryModels) {
            batteryModel.setClick(false);
            operateAdapter.notifyDataSetChanged();
        }
    }

    private void registerEvent() {
        LiveEventBus.get(EventDef.EVENT_USER_FACE_POER_OK, String.class).observe(this, observerOk);
    }

    /**
     * 刷新电池信息
     */
    private void refreshData() {
        for (BatteryModel batteryModel : batteryModels) {
            Integer storeNo = batteryModel.getStoreNo();
            BatteryStore batteryStore = DeviceService.inst.getBatteryStoreByStoreNo(storeNo);
            batteryModel.setBatteryLogo(batteryStore.getHasBattery() ? 1 : 0);
            if (batteryModel.getType() == 1) {
                //取
                if (batteryStore.getHasBattery()) {
                    batteryModel.setBatteryLogo(1);
                    batteryModel.setBatteryTip(String.format("预计行驶%s公里", batteryStore.getBatteryMileage()));
                    batteryModel.setOperateTip("取");
                } else {
                    batteryModel.setBatteryLogo(4);
                    batteryModel.setBatteryTip("");
                    batteryModel.setOperateTip("空");
                }
            } else {
                //存
                if (batteryStore.getHasBattery()) {
                    batteryModel.setBatteryLogo(1);
                    batteryModel.setOperateTip("不可存");
                } else {
                    batteryModel.setBatteryLogo(4);
                    batteryModel.setOperateTip("存");
                }
            }
            operateAdapter.notifyDataSetChanged();
        }
    }

    private void setView() {
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        RecyclerView.ItemDecoration gridItemDecoration = new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int layoutPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
                if (layoutPosition != 0 && layoutPosition != 1) {
                    outRect.top = 20;
                }
            }
        };

        recyclerView.addItemDecoration(gridItemDecoration);
        batteryModels = new ArrayList<>();
        batteryModels.add(new BatteryModel(2, "", "存", "01", 3, false, false, 2));
        batteryModels.add(new BatteryModel(1, "", "取", "04", 6, false, true, 1));
        batteryModels.add(new BatteryModel(2, "", "已选", "02", 2, false, false, 2));
        batteryModels.add(new BatteryModel(4, "", "空", "05", 5, false, true, 1));
        batteryModels.add(new BatteryModel(1, "", "不可存", "03", 1, false, false, 2));
        batteryModels.add(new BatteryModel(3, "预计行驶10公里", "取", "06", 4, false, true, 1));
        operateAdapter = new OperateAdapter(batteryModels);
        recyclerView.setAdapter(operateAdapter);

        refreshData();

        operateAdapter.setItemClickCallback(new OperateAdapter.ItemClickCallback() {
            @Override
            public void handleItemClick(BatteryModel batteryModel) {
                Integer storeNo = batteryModel.getStoreNo();
                if (storeNo < 1 || storeNo > 6) {
                    return;
                }

                List<Integer> list = Stream.of(1, 2, 3).collect(Collectors.toList());
                boolean contains = list.contains(storeNo);
                if (contains) {
                    //存电
                    LiveEventBus.get(EventDef.EVENT_USER_FACE_RETURN_OPER, BatteryModel.class).post(batteryModel);
                } else {
                    //取电
                    LiveEventBus.get(EventDef.EVENT_USER_FACE_TAKE_OPER, BatteryModel.class).post(batteryModel);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LiveEventBus.get(EventDef.EVENT_USER_FACE_POER_OK, String.class).removeObserver(observerOk);
    }
}
